# coding=utf-8
from datetime import datetime

from src.db.dbhelper import query_sql_by_pandas, create_total_sql, create_page_sql, get_first_scaner, execute_sql_new


def get_user(*args, **kwargs):
    limit = kwargs.get('limit')
    page = kwargs.get('page')
    username = kwargs.get("username")
    sql = "select * from sys_user "
    if username is not None and username != '':
        sql += f" where uname like '%%{username}%%' "
    total_sql = create_total_sql(sql)
    sql = create_page_sql(sql, page, limit)
    df = query_sql_by_pandas(sql)
    total_df = query_sql_by_pandas(total_sql)
    total = int(get_first_scaner(total_df))
    return df, total


def del_user_by_id_mapper(*args, **kwargs):
    user_id = kwargs.get('id')
    print(user_id)
    sql = "delete from sys_user where id = %s"
    return execute_sql_new(sql, (user_id,))


def add_user_mapper(*args, **kwargs):
    uname = kwargs.get("uname")
    email = kwargs.get("email")
    content = kwargs.get("content")
    password = kwargs.get("password")
    sql = "insert into sys_user(uname, upwd, uemail, content, create_time) values(%s,%s,%s,%s,%s) "
    return execute_sql_new(sql, (uname, password, email, content, datetime.now()))


def update_user_mapper(*args, **kwargs):
    uname = kwargs.get("uname")
    email = kwargs.get("email")
    content = kwargs.get("content")
    password = kwargs.get("password")
    uid = kwargs.get("uid")
    sql = "update sys_user set uname=%s, upwd=%s, uemail=%s, content=%s, create_time=%s where id=%s "
    return execute_sql_new(sql, (uname, password, email, content, datetime.now(), uid))
